package com.spring.boot.springBoot.threadPool;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * Created by pactera on 2018/1/8.
 */
@Slf4j
@RestController
public class ThreadPoolController {

    @Autowired
    private MyTask myTask;

    @GetMapping("/pool")
    public Map<String,String> testThreadPool() throws Exception{
            myTask.doTask(10);
       return new HashMap<>();
    }
    @GetMapping("/futurePool")
    public Map<String,String> testFuturePool() throws Exception{
        Map<String,String> result = new HashMap<>();
        List<Future<String>> lstFuture = new ArrayList<>();// 存放所有的线程，用于获取结果
        for(int i = 0;i < 100;i++){
            Future<String> future = myTask.asyncFutureResult(i);
            lstFuture.add(future);
        }

        for(Future<String> future:lstFuture){
            if(future.isCancelled()){
                 log.info("线程被取消");
            }
            if(future.isDone()){
                log.info(future.get());
            }
        }
       return result;
    }
}
